
package ui.activity.poscontrol.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.View;

import net.ck.hzq.R;
import net.ck.hzq.TobuyApplication;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import Utils.ToastUtil;
import base.BaseViewHolder;
import base.SuperBaseAdapter;
import event.PosAllotEvent;
import ui.activity.order.OrderDetailsAct;
import ui.activity.poscontrol.beanmodel.MyPosBean;


/**
 *POS查询列表适配器
 * Created by chenjian on 19/2/27
 */
public class NotActiveAdapter extends SuperBaseAdapter<MyPosBean.RecordsBean> {

    List<MyPosBean.RecordsBean> mData = new ArrayList<>();
    private List<String>  selectPosition = new ArrayList<>();//选择的条目
    private List<String> mSelectedDevices = new ArrayList<>();
    Context mContext;
    private int mMaxBackAmount = 0;
    private int piliangstartposition = -1;//批量开始坐标
    private int piliangendposition = -1;//批量结束坐标
    public NotActiveAdapter(Context context, List<MyPosBean.RecordsBean> data) {
        super(context, data);
        mContext = context;
        mData = data;
    }
    public void refresh() {
        mMaxBackAmount = 0;
        selectPosition.clear();
        mSelectedDevices.clear();
    }

    @Override
    protected void convert(BaseViewHolder holder, final MyPosBean.RecordsBean item, final int position) {
        holder.setText(R.id.index, (position+1)+"")
                .setText(R.id.device_no, item.getSn())//终端编号
                .setText(R.id.return_amount_range, "0-"+ Math.round(Float.valueOf(item.getBackAmount()+"")))//划拨额度
                .setText(R.id.return_usedate, item.getRemainingDay()+"");//截止日期
        if(item.getRemainingDay()<=10){
            holder.setTextColorRes(R.id.return_usedate,R.color.red_fe3333);
        }else {
            holder.setTextColorRes(R.id.return_usedate,R.color.tc_666666);
        }
        if (selectPosition.indexOf(position+"") > -1) {
            holder.getView(R.id.cb).setBackgroundResource(R.mipmap.checkedbox);
        } else {
            holder.getView(R.id.cb).setBackgroundResource(R.mipmap.checkbox);
        }
        if(mData.get(position).getType() == 1){
            holder.setBackgroundResource(R.id.total_linear,R.color.blueback);
            holder.getView(R.id.return_usedate_godetail).setVisibility(View.INVISIBLE);
        }else{
            holder.setBackgroundResource(R.id.total_linear,R.color.white);
            holder.getView(R.id.return_usedate_godetail).setVisibility(View.VISIBLE);
        }



        holder.getView(R.id.order_sn).setOnClickListener(v -> {
            if(mData.get(position).getType() != 1) {
                Intent intent = new Intent(mContext, OrderDetailsAct.class);
                intent.putExtra("id", item.getOrderId() + "");
                mContext.startActivity(intent);
            }
        });
        holder.getView(R.id.layout4).setOnClickListener(v -> {
            if(TobuyApplication.isPiliang){
                if(piliangstartposition==-1&&piliangendposition == -1){
                    piliangstartposition = position;
                    if (mSelectedDevices.indexOf(item.getSn()) < 0) {
                        mSelectedDevices.add(item.getSn());
                        selectPosition.add(position + "");
                        holder.getView(R.id.cb).setBackgroundResource(R.mipmap.checkedbox);

                    } else {
                        mSelectedDevices.remove(item.getSn());
                        selectPosition.remove(position + "");
                        holder.getView(R.id.cb).setBackgroundResource(R.mipmap.checkbox);
                        if (mSelectedDevices.size() == 0) {
                            mMaxBackAmount = 0;
                        }
                    }
                    ToastUtil.show(mContext,"批量起点已选择");
                }else  if(piliangstartposition!=-1&&piliangendposition == -1) {
                    ToastUtil.show(mContext,"批量终点已选择，已完成批量选择！");
                    piliangendposition = position;
                    if(piliangstartposition!=piliangendposition) {
                        if(piliangstartposition<piliangendposition){
                            for (int i = piliangstartposition + 1; i < piliangendposition + 1; i++) {
                                if (mSelectedDevices.indexOf(mData.get(i).getSn()) < 0) {
                                    mData.get(i).setIsselect(1);
                                    mSelectedDevices.add(mData.get(i).getSn());
                                    selectPosition.add(i + "");
                                } else {
                                    mSelectedDevices.remove(mData.get(i).getSn());
                                    selectPosition.remove(i + "");
                                    mData.get(i).setIsselect(0);
                                    if (mSelectedDevices.size() == 0) {
                                        mMaxBackAmount = 0;
                                    }
                                }
                            }
                        }else{
                            for (int i = piliangendposition ; i < piliangstartposition; i++) {
                                if (mSelectedDevices.indexOf(mData.get(i).getSn()) < 0) {
                                    mData.get(i).setIsselect(1);
                                    mSelectedDevices.add(mData.get(i).getSn());
                                    selectPosition.add(i + "");
                                } else {
                                    mSelectedDevices.remove(mData.get(i).getSn());
                                    selectPosition.remove(i + "");
                                    mData.get(i).setIsselect(0);
                                    if (mSelectedDevices.size() == 0) {
                                        mMaxBackAmount = 0;
                                    }
                                }
                            }
                        }

                        piliangstartposition = -1;
                        piliangendposition = -1;
                        notifyDataSetChanged();
                    }else{
                        piliangstartposition = -1;
                        piliangendposition = -1;
                        mData.get(position).setIsselect(0);
                        if (mSelectedDevices.indexOf(item.getSn()) < 0) {
                            mSelectedDevices.add(item.getSn());
                            selectPosition.add(position + "");
                            holder.getView(R.id.cb).setBackgroundResource(R.mipmap.checkedbox);

                        } else {
                            mSelectedDevices.remove(item.getSn());
                            selectPosition.remove(position + "");
                            holder.getView(R.id.cb).setBackgroundResource(R.mipmap.checkbox);
                            if (mSelectedDevices.size() == 0) {
                                mMaxBackAmount = 0;
                            }
                        }
                    }
                }
            }else {
                piliangstartposition = -1;
                piliangendposition = -1;
                if (mSelectedDevices.indexOf(item.getSn()) < 0) {
                    mSelectedDevices.add(item.getSn());
                    selectPosition.add(position + "");
                    holder.getView(R.id.cb).setBackgroundResource(R.mipmap.checkedbox);

                } else {
                    mSelectedDevices.remove(item.getSn());
                    selectPosition.remove(position + "");
                    holder.getView(R.id.cb).setBackgroundResource(R.mipmap.checkbox);
                    if (mSelectedDevices.size() == 0) {
                        mMaxBackAmount = 0;
                    }
                }
            }
            int tag = 0;
            for (int i =0;i<mSelectedDevices.size();i++){
                int positionnum = 0;
                for (int j = 0; j<mData.size();j++)
                {
                    if(mSelectedDevices.get(i).equals(mData.get(j).getSn())){
                        positionnum  = Math.round(Float.valueOf(mData.get(j).getBackAmount()+""));
                    }
                }

                if(tag>positionnum||tag == 0){
                    tag = positionnum;
                }
            }
            mMaxBackAmount = tag;
            EventBus.getDefault().post(new PosAllotEvent(mMaxBackAmount, mSelectedDevices));
        });

    }



    @Override
    protected int getItemViewLayoutId(int position, MyPosBean.RecordsBean item) {
        return R.layout.item_myposnotactive_list;
    }

}
